package com.redis.seckill.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.redis.seckill.entity.MyOrder;
import com.redis.seckill.entity.Product;
import com.redis.seckill.entity.User;
import com.redis.seckill.lang.Renum;
import com.redis.seckill.mapper.MyOrderMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redis.seckill.mq.service.Sender;
import com.redis.seckill.service.MyOrderService;
import com.redis.seckill.service.ProductService;
import com.redis.seckill.service.UserService;
import com.redis.seckill.utils.RedisUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2020-10-28
 */
@Transactional
@Service
@Log4j2
public class MyOrderServiceImpl extends ServiceImpl<MyOrderMapper, MyOrder> implements MyOrderService {


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private Sender sender;

    @Override
    public boolean seckill(Integer userId, Integer productId, Integer quantity) {

        boolean result = true;
//        获取redis中商品的库存容量
        String jsonProduct = JSONObject.toJSONString(redisUtil.get("productInfo" + productId));
        Product product = JSONObject.parseObject(jsonProduct, Product.class);

        if (product.getProductResidue()<=0){
            result = false;
            log.error("【秒杀下单service】,库存不足");
        }
        sender.sendDirectQueue(userId,productId,quantity);
        return result;
    }
}
